package chatroom18;



public class Message {
	byte type;
	byte length;
	String content;
	
	Message(byte Type, String Message)
	{
		type = Type;
		content = Message;
		length = (byte) (content.getBytes().length);
	}
	
	Message()
	{
		type = 1;
		content = "";
		length = 0;
	}
	
	Message(byte[] message)
	{
		type = message[0];
		length = message[1];
		content = new String(message, 2, length);
	}
	
	public byte[] toByte()
	{
		byte[] buf = new byte[256];
		byte[] tmp = new byte[2];
		tmp[0]=type;
		tmp[1] = length;
		buf = append(tmp, content.getBytes() );
		return buf;
	}
	
	private byte[] append(byte[] bit1,byte[] bit2)
	{
		byte[] buf = new byte[bit1.length+bit2.length];
		int i;
		for(i=0; i<bit1.length;i++)
		{
			buf[i] = bit1[i];
		}
		for(int j = 0; j < bit2.length; j++)
		{
			buf[j+i] = bit2[j];
		}
		return buf;
	}

}
